package com.newsproject.controller;

import com.newsproject.pojo.Result;
import com.newsproject.service.PostService;

import io.swagger.annotations.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/category")
@Api(tags="导航栏的接口")
public class CategoryPage {

    @Autowired
    private PostService postService;

    @GetMapping("/{categoryId}")
    @ApiOperation(value = "跳转到分类页面",notes = "根据分类id跳转到分类页面")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "1.posts :文章列表 2.hasMore : 是否有更多文章 ，boolean 类型")
    })
    public ModelAndView redirectToPage(
            @ApiParam("类别的id")@PathVariable Integer categoryId,
            @ApiParam("页码数") @RequestParam(defaultValue = "1")Integer pageNum ,
            @ApiParam("每页条数") @RequestParam(defaultValue = "10") Integer pageSize,
            HttpSession session) {
        return    postService.getPostByPageAndTagId(pageNum,pageSize, categoryId,session);
    }

    @ApiOperation(value = "根据标签查询文章",notes = "根据标签查询文章")
    @GetMapping("/findPost/{categoryId}")
    @ResponseBody
    @ApiResponses(value = {@ApiResponse(code = 200,
            message = "posts:文章",
            response =String.class )})
    public Result<Map<String, Object>> findPostByPostTypeAndUserId(
            @ApiParam("文章类型")@PathVariable("categoryId") Integer tagId,
            @RequestParam(defaultValue = "1")Integer pageNum,
            @RequestParam(defaultValue = "10")Integer pageSize,
            HttpSession session
    ){
        if(tagId==2){
            return postService.getFollowerPost(session,pageNum,pageSize);
        }
       return postService.findPostByPageAndTagId(pageNum,pageSize,tagId);
    }

}
